package connevent

import (
	"fmt"
	"zinxtcp/default/message"
	"zinxtcp/ziface"
)

type ConnEvent struct{}

func (ce *ConnEvent) GetConnBegin() func(ziface.IConnection) {
	return DoConnectionBegin
}

func (ce *ConnEvent) GetConnStop() func(ziface.IConnection) {
	return DoConnectionLost
}

//创建连接的时候执行
func DoConnectionBegin(conn ziface.IConnection) {
	fmt.Println("DoConnecionBegin is Called ... ")
	fmt.Println("Set conn Name, Home done!")
	conn.SetProperty("Name", "Aceld")
	conn.SetProperty("Home", "https://www.jianshu.com/u/35261429b7f1")
	packHeader := message.NewPackHeader(2)
	data := []byte("DoConnection BEGIN...")
	err := conn.SendMsg(packHeader, data)
	if err != nil {
		fmt.Println(err)
	}
}

//连接断开的时候执行
func DoConnectionLost(conn ziface.IConnection) {
	if name, err := conn.GetProperty("Name"); err == nil {
		fmt.Println("Conn Property Name = ", name)
	}

	if home, err := conn.GetProperty("Home"); err == nil {
		fmt.Println("Conn Property Home = ", home)
	}

	fmt.Println("DoConneciotnLost is Called ... ")
}
